// eventBus

class EventBus {
    constructor(debug) {
        this.DEBUG = debug;
        this.storage = {};
        this.eventList = {};

    }
    // Caller传入调用者名字
    on(eventName,caller,cb) {
        let list = this.eventList[eventName] || [];
        list.push(cb);
        this.eventList[eventName] = list;
        if(this.DEBUG) {
            console.log(`${caller}注册事件: ${eventName}`);
        }
    }
    emit(eventName,caller,...args) {
        if(this.DEBUG) {
            console.log(`${caller}调用事件: ${eventName}`);
        }
        this.eventList[eventName] = this.eventList[eventName] || [];
        for(let x of this.eventList[eventName]) {
            x(...args);
        }
    }
}

let eventBus = new EventBus(true);


export default function() {
    return eventBus;
}